package com.ruoyi.teaching.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.teaching.bo.YaHolidayDetailBo;
import com.ruoyi.teaching.domain.YaHoliday;
import com.ruoyi.teaching.mapper.YaHolidayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaHolidayConfigMapper;
import com.ruoyi.teaching.domain.YaHolidayConfig;
import com.ruoyi.teaching.service.IYaHolidayConfigService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 节假日配置Service业务层处理
 * 
 * @author ZBX
 * @date 2025-02-22
 */
@Service
public class YaHolidayConfigServiceImpl implements IYaHolidayConfigService 
{
    @Autowired
    private YaHolidayConfigMapper yaHolidayConfigMapper;

    @Autowired
    private YaHolidayMapper yaHolidayMapper;

    /**
     * 查询节假日配置
     * 
     * @param id 节假日配置主键
     * @return 节假日配置
     */
    @Override
    public YaHolidayConfig selectYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigById(id);
    }

    /**
     * 查询节假日配置列表
     * 
     * @param yaHolidayConfig 节假日配置
     * @return 节假日配置
     */
    @Override
    public List<YaHolidayConfig> selectYaHolidayConfigList(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigList(yaHolidayConfig);
    }

    /**
     * 新增节假日配置
     * 
     * @param yaHolidayDetailBoList 节假日配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertYaHolidayConfig(List<YaHolidayDetailBo> yaHolidayDetailBoList) {
        // 数据校验，判断是否为空，使用apache集合工具类
        if (CollectionUtils.isEmpty(yaHolidayDetailBoList) || yaHolidayDetailBoList.size() != 7) {
            throw new ServiceException("节假日配置数据结构有问题");
        }

        // 1.先新增节假日配置（1方）
        YaHolidayConfig config = new YaHolidayConfig();
        // 根据国庆节的起始时间获取到配置年份
        YaHolidayDetailBo yaHolidayDetailBo = yaHolidayDetailBoList.get(6);
        String year = yaHolidayDetailBo.getDate().toString().substring(0, 4);
        config.setYear(year);

        // 将 yaHolidayDetailBoList 使用 Jackson 转换成 json 字符串
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            config.setContent(objectMapper.writeValueAsString(yaHolidayDetailBoList));
        } catch (JsonProcessingException e) {
            throw new ServiceException("转换json异常");
        }

        // 保存到数据库
        int rows = yaHolidayConfigMapper.insertYaHolidayConfig(config);

        // 2.根据节假日配置动态生成多个节假日（多方）；维护关系
        List<YaHoliday> holidayList = new ArrayList<>();
        // 遍历 yaHolidayDetailBoList 获取每个节假日的开始时间和持续天数
        for (YaHolidayDetailBo bo : yaHolidayDetailBoList) {
            // 获取到每个节假日的开始时间和持续天数
            String startDate = bo.getDate();
            // 将 startDate 转换成 Date 类型
            LocalDate startDateDate = LocalDate.parse(startDate);
            Integer count = bo.getCount();
            // 3.根据每个节假日的开始时间和持续天数，动态生成多个节假日（多方）
            for (int i = 0; i < count; i++) {
                // 生成节假日
                YaHoliday holiday = new YaHoliday();
                holiday.setDate(startDateDate.toString());
                holiday.setWeekday(startDateDate.getDayOfWeek().getValue());
                holiday.setConfigId(config.getId());
                holiday.setDescription(bo.getDescription());
                holidayList.add(holiday);
                // 获取到下一天的时间
                startDateDate = startDateDate.plusDays(1);
            }
        }

        // 批量插入节假日
        if (!holidayList.isEmpty()) {
            rows += yaHolidayMapper.batchInsertYaHoliday(holidayList);
        }

        return rows;
    }


    /**
     * 修改节假日配置
     * 
     * @param yaHolidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int updateYaHolidayConfig(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.updateYaHolidayConfig(yaHolidayConfig);
    }

    /**
     * 批量删除节假日配置
     * 
     * @param ids 需要删除的节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigByIds(Long[] ids)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigByIds(ids);
    }

    /**
     * 删除节假日配置信息
     * 
     * @param id 节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigById(id);
    }
}
